// pages/order/order.js
var api = require("../../utils/api.js");
var util = require("../../utils/util.js");
//获取应用实例
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    taxpayers:true
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      windowHeight: app.globalData.windowHeight
    })

    if (options.oid) {
      this.setData({
        oid: options.oid
      })
    }else{
      var oid = options.q;
      oid = unescape(decodeURI(oid));
      oid = oid.substring(oid.lastIndexOf("=") + 1);
      this.setData({
        oid: oid
      })
    }

    this.authorization();
    util.showLoading("加载中...");

  },
  onShow:function(){

  },


//购物车请求
cartList: function () {
  var params = new Object()
  var that = this;
  var carNum=0;
  var total=0;
  params.oid = this.data.oid
  api.POST({
    params: params,
    url: app.globalData.base_url + 'order/info',
    success: function (res) {
      util.hiddenLoading();
      if (util.isEmpty(res.data.code)) {
        
        that.setData({
          carts: res.data.data.productList,
          total: res.data.data.total_fee,
          privilege: res.data.data.privilege
        })
      }
    },
    fail: function (fail) {
      util.showError("登录失败，请检查网络");

    }
  })
},

// taxpayersCol:function(){
//   var taxpayers = this.data.taxpayers;
//   if (taxpayers){
//     this.setData({
//       taxpayers:false
//     })
//   }else{
//     this.setData({
//       taxpayers: true
//     })
//   }
// },


  //支付接口
  wxPayOrder:function(oid){
    var params = new Object()
    var that = this;
    params.oid=oid;
    api.POST({
      params: params,
      url: app.globalData.base_url + 'payment/getWechatInfo',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {


          var preData = res.data.data.payinfo;
          wx.requestPayment({
            timeStamp: preData.timeStamp,
            nonceStr: preData.nonceStr,
            package: preData.package,
            signType: preData.signType,
            paySign: preData.paySign,
            success: function (res) {
              wx.redirectTo({
                url: '../paySuccess/paySuccess?oid=' + oid
              })
            },
            fail: function (error) {
              util.showError("支付失败");
                 setTimeout(function(){
                   wx.switchTab ({
                     url: '../index/index'
                   })
                 },1000)
            }
          })
        } else {
          util.showError(res.data.message);
          setTimeout(function () {
            wx.switchTab ({
              url: '../index/index'
            })
          }, 1000)
        }
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");
      }
    })
  },


  // 授权
  authorization: function () {
    var that = this
    wx.login({
      success: function (res_login) {
        wx.getUserInfo({
          withCredentials: true,
          success: function (res_user) {
            var login_code = res_login.code;
            var login_encryptedData = res_user.encryptedData;
            var login_iv = res_user.iv;
            that.autoLogin(login_code, login_encryptedData, login_iv);
          }, fail: function (fail_user) {
            that.showModal();
          }
        })
      }, fail: function (fail_login) {
        console.log("fail");
      }
    })
  },

  // 重新授权
  reAuthorization: function () {
    var that = this
    wx.openSetting({
      success: function (res) {
        if (res.authSetting["scope.userInfo"]) {
          that.authorization();
        }
      }
    })
  },

  //未登录警告框
  showModal: function () {
    var that = this
    wx.showModal({
      title: '警告',
      content: '若不授权登录，则无法使用',
      confirmText: '授权',
      cancelText: '不授权',
      success: function (suress) {
        if (suress.confirm) {
          that.reAuthorization();
        } else {
          that.showModal();
        }
      }
    })
  },

  //登录请求
  autoLogin: function (login_code, login_encryptedData, login_iv) {
    var that = this;
    var params = new Object()
    params.code = login_code;
    params.encryptedData = login_encryptedData;
    params.iv = login_iv;

    api.POST({
      params: params,
      url: app.globalData.base_url + 'user/login',
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          app.globalData.accountInfo = res.data.data.result;
          var oid = that.data.oid;
          that.cartList();
          that.orderInfo(oid);
          that.wxPayOrder(oid);
        }
      },
      fail: function (fail) {
        util.showError("登录失败，请检查网络");

      }
    })
  },
  //订单信息
  orderInfo: function (oid) {
    var that = this;
    api.POST({
      params: { oid: oid },
      url: app.globalData.orderInfo_url,
      success: function (res) {
        if (util.isEmpty(res.data.code)) {
          wx.setStorageSync('orderData', res.data.data)
        }
      }
    })

  },
})